package com.learn.boot.interceptor;

import com.learn.boot.domain.Users;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 拦截器
 * @author:         MI
 * @email:          448341911@qq.com
 * @createDate:     2023/11/11 22:48
 * @updateUser:     MI
 * @updateDate:     2023/11/11 22:48
 * @updateRemark:   修改内容
 * @version:        v1.0
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 在HandlerMapping确定适当的处理程序对象之后，但在HandlerAdapter调用处理程序之前调用.
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 在处理程序执行之前调用
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");

        //获取请求的RUi:去除http:localhost:8080这部分剩下的
        String uri = request.getRequestURI();
        //UTL:除了login.jsp是可以公开访问的，其他的URL都进行拦截控制
        if (uri.indexOf("login")+uri.indexOf("register") >= 0) {
            return true;
        }
        //获取session
        HttpSession session = request.getSession();
        Users user = (Users) session.getAttribute("user");

        //判断session中是否有用户数据，如果有，则返回true，继续向下执行
        if (user != null) {
            return true;
        }
        //不符合条件的给出提示信息，并转发到登录页面，这里.getRequestDispatcher()的路径换成自己项目的首页路径。parameter是一个给jsp页面显示的参数，在jsp页面用${parameter}就能显示。
        request.setAttribute("parameter", "您还没有登录，请先登录！");
        request.getRequestDispatcher("/user/login").forward(request, response);
        log.info("==============================HandlerInterceptor preHandle [" + handler + "]==============================");
        log.info("------------------>:已完成跨域处理");
        return true;
    }
    /**
     * 在HandlerAdapter实际调用处理程序之后调用，但在DispatcherServlet呈现视图之前调用.
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 在处理程序执行后调用
        log.info("==============================HandlerInterceptor postHandle [" + handler + "]==============================");
        log.info("Request URL: " + request.getRequestURL());
    }
    /**
     * 在HandlerAdapter实际调用处理程序之后调用，但在DispatcherServlet呈现视图之前调用.
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 在请求完成后调用
        log.info("\n-------- LogInterception.afterCompletion --- ");
        log.info("==============================HandlerInterceptor afterCompletion [" + handler + "]==============================");
        long endTime = System.currentTimeMillis();
        log.info("Request URL: " + request.getRequestURL());
        log.info("End Time: " + endTime);
    }
}
